<template>
  <div class="fundRecords divBox">
    <el-card class="box-card" shadow="hover">
      <template class="clearfix" v-slot:header>
        <div class="container">
          <el-form :model="capitalFrom" size="small" label-width="74px">
            <el-form-item label="提现状态：">
              <el-radio-group v-model="capitalFrom.type" size="small" @change="getCapitalRecordsList('1')">
                <el-radio-button :label="-99">全部</el-radio-button>
                <el-radio-button :label="0">购买记录</el-radio-button>
                <el-radio-button :label="1">佣金记录</el-radio-button>
                <el-radio-button :label="2">佣金提现记录</el-radio-button>
              </el-radio-group>
            </el-form-item>
            <el-row>
              <el-col v-bind="grid">
                <el-form-item label="昵称/ID：" class="search-select">
                  <el-input v-model="capitalFrom.keyword" placeholder="请输入昵称/ID" size="small" clearable>
                    <el-button slot="append" icon="el-icon-search" size="small" @click="getCapitalRecordsList('1')"></el-button>
                  </el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
        <el-button size="small" icon="el-icon-upload2">导出</el-button>
      </template>
      <el-table
        v-loading="fundRecordLoading"
        :data="fundRecordList"
        style="width: 100%;"
        size="mini"
        class="table"
      >
        <el-table-column label="用户ID" sortable :sort-method="sortById" width="100">
          <template slot-scope="{ row }">
            <div>
              <span>{{ row.user.id }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="昵称" min-width="130">
          <template slot-scope="{ row }">
            <div>{{ row.user.realName || row.user.nickName }}</div>
          </template>
        </el-table-column>
        <el-table-column label="金额" min-width="120">
          <template slot-scope="{ row }">
            <div class="fundRecord">
              <p v-if="row.pm === 1" :class="row.pm === 0 ? 'fontColorRed' : 'fontColorGreen'">{{ row.number | NumFormat }}</p>
              <p :class="row.pm === 0 ? 'fontColorRed' : 'fontColorGreen'" v-else>-{{ row.number | NumFormat }}</p>
            </div>
          </template>>
        </el-table-column>
        <el-table-column label="明细类型" min-width="100" prop="title"></el-table-column>
        <el-table-column label="备注" min-width="200" prop="remark"></el-table-column>
        <el-table-column label="创建时间" min-width="150">
          <template slot-scope="{ row }">
            <span>{{ row.createDateUTC | uctTimeFileter }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="replyBox">
        <pagination
          v-show="pagination.total > 0"
          :total="pagination.total"
          :page.sync="pagination.page"
          :limit.sync="pagination.limit"
          :page-sizes="[10, 20, 30, 50]"
          @pagination="fundRecordChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
  import {
    getCapitalRecordsListApi
  } from '@/api/financial';
  import { NumFormat } from '@/filters/commFilter'

  export default {
    name: "index",
    data() {
      return {
        grid: {
          xl: 5,
          lg: 7,
          md: 12,
          sm: 12,
          xs: 24
        },
        fundRecordLoading: false,
        fundRecordList: [],
        capitalFrom: {
          keyword: '',
          type: -99
        },
        pagination: {
          page: 1,
          limit: 10,
          total: 0
        }
      };
    },
    mounted() {
      this.getCapitalRecordsList();
    },
    methods: {
      // 导出
      handleDownload() {
        import('@/vendor/Export2Excel').then(excel => {
          const tHeader = ['会员ID', '昵称', '金额/积分', '类型', '备注', '创建时间'];
          const filterVal = ['id', 'nickName', 'number', 'title', 'remark', 'createDateUTC'];
          const list = this.fundRecordList.map((item, index) => {
            return {
              id: item.user.id,
              nickName: item.user.realName || item.user.nickName,
              number: NumFormat(item.number),
              title: item.title,
              remark: item.remark,
              createDateUTC: this.$moment.utc(item.payDateUTC).tz(this.$moment.tz.guess()).format('YYYY-MM-DD HH:mm:ss')
            };
          });
          const data = this.formatJson(filterVal, list);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: `资金监控_${ new Date().getTime() }`,
            autoWidth: true,
            bookType: 'xlsx'
          })
        })
      },
      formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => {
          if (j === 'user') {
            return parseTime(v[j])
          } else {
            return v[j]
          }
        }))
      },
      // 表格排序
      sortById(sort1, sort2) {
        return sort1.user.id - sort2.user.id;
      },
      /**
       * 获取资金记录
       */
      getCapitalRecordsList(num) {
        this.$set(this, 'fundRecordLoading', true);
        getCapitalRecordsListApi({
          pageNumber: num ? num : this.pagination.page,
          pageSize: this.pagination.limit,
          orderBy: 'Id desc',
          keyword: this.capitalFrom.keyword,
          type: this.capitalFrom.type === -99 ? -1 : this.capitalFrom.type
        }).then(res => {
          this.$set(this, 'fundRecordList', res.data);
          this.$set(this.pagination, 'total', res.head.totalCount);
          this.$set(this, 'fundRecordLoading', false);
        }).catch(() => {
          this.$set(this.pagination, 'total', 0);
          this.$set(this, 'fundRecordList', []);
          this.$set(this, 'fundRecordLoading', false);
        });
      },
      fundRecordChange(apply) {
        this.$set(this.pagination, 'page', apply.page);
        this.$set(this.pagination, 'limit', apply.limit);
        this.getCapitalRecordsList();
      }
    }
  }
</script>

<style lang="scss" scoped>
  .fundRecord {
    .fontColorRed {
      color: #ff5722;
    }
    .fontColorGreen {
      color: #03C55B;
    }
  }
</style>
